package com.yl.routeing.service;

import com.graphhopper.GHRequest;
import com.graphhopper.GHResponse;
import com.graphhopper.GraphHopper;
import com.graphhopper.ResponsePath;
import com.graphhopper.util.Instruction;
import com.graphhopper.util.InstructionList;
import com.graphhopper.util.PointList;
import com.graphhopper.util.Translation;
import com.yl.routeing.config.Coordinate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

@Service
public class MinStepCalculateService {

    @Autowired
    private GraphHopper hopper;

    public List<Coordinate> routing(double fromLat, double fromLon, double toLat, double toLon) {
        GHRequest req = new GHRequest(fromLat, fromLon, toLat, toLon);
        req.setProfile("car").setLocale(Locale.CHINESE);
        //计算路线
        GHResponse rsp = hopper.route(req);
        if (rsp.hasErrors())
            throw new RuntimeException(rsp.getErrors().toString());
        ResponsePath path = rsp.getBest();
        PointList pointList = path.getPoints();
        double distance = path.getDistance();
        long timeInMs = path.getTime();
        Translation tr = hopper.getTranslationMap().getWithFallBack(Locale.CHINESE);
        InstructionList il = path.getInstructions();
        // 导航路线的转向说明
        for (Instruction instruction : il) {
            System.out.println("距离 " + instruction.getDistance() + "; 路线转向说明: " + instruction.getTurnDescription(tr));
        }
        System.out.println("路线总距离：" + distance + "（米）");
        System.out.println("大概花费时间：" + (timeInMs) / 1000 + "（秒）");
        List<Coordinate> list = new ArrayList<>();
        pointList.forEach(coor -> list.add(new Coordinate(coor.getLat(), coor.getLon())));
        return list;
    }

}
